<?php

namespace app\admin\controller;

use app\admin\model\SectionDepartmentMajor;

/**
 * Class Budget
 * @package app\admin\controller
 * 科系控制器
 */
class SectionDepartment extends Basic
{

    /**
     * 初始化
     */
    public function _initialize()
    {
        parent::_initialize();
    }


    /**
     * 添加科系
     */
    public function add_section_department()
    {
        $name = input('name');
        $sort = input('sort', 1);
        $status = input('status', 1);
        $major_ids = input('major_ids/a');
        if (!$name) {
            json_fail('缺少标题参数');
        }
        if (!$major_ids) {
            json_fail('请先选择专业');
        }
        //实例化数据库
        $section_department = new \app\admin\model\SectionDepartment();
        $section_department_major = new SectionDepartmentMajor();

        //要添加的数据
        $addData = array(
            'name' => $name,
            'sort' => $sort,
            'create_time'=>time(),
            'status' => $status
        );

        $section_department_id = $section_department->insertGetId($addData);
        if (!$section_department_id) {
            json_fail('存储失败');
        }
        foreach ($major_ids as $k=>$v){
            $addSDMData=  array(
                'section_department_id'=>$section_department_id,
                'major_id'=>$v
            );
            $section_department_major->insertGetId($addSDMData);
        }
        json_success($section_department_id);
    }

    /**
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 科系列表
     * @author chengkaikai
     */
    public function section_department_list()
    {
        $keys = input('keys');
        $page = input('page', 1);
        $limit = input('limit', 10);

        //实例化数据库
        $section_department = new \app\admin\model\SectionDepartment();
        $section_department_major = new SectionDepartmentMajor();
        $major = new \app\admin\model\Major();

        $map['status'] = array('neq', 0);
        if ($keys) {
            $map['name'] = array('like', '%' . $keys . '%');
        }

        $list = $section_department->where($map)->order('sort')->page($page, $limit)->select();
        if (!$list) {
            $infoList = array(
                'list' => [],
                'count' => 0
            );
            json_success($infoList);
        }

        foreach ($list as $k=>$v){
            $major_ids =$section_department_major->where(array('section_department_id'=>$v['id'],'status'=>1))->column('major_id');
            if(!$major_ids){
                $v['major_name'] = [];
            }else{
                $v['major_name'] = $major->where(array('id'=>array('in',$major_ids)))->column('name');
            }
            $list[$k]=$v;
        }
        $infoList = array(
            'list' => $list,
            'count' => $section_department->where($map)->count()
        );
        json_success($infoList);
    }

    /**
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @author chengkaikai
     * 展示修改
     */
    public function show_edit()
    {
        $section_department_id = input('section_department_id');
        if (!$section_department_id) {
            json_fail('缺少科系id参数');
        }

        //实例化数据库
        $section_department = new \app\admin\model\SectionDepartment();
        $section_department_major = new SectionDepartmentMajor();

        $info = $section_department->where(array('id' => $section_department_id))->find();
        if (!$info) {
            json_fail('暂无数据');
        }
        $info['major_ids'] =  $section_department_major->where(array('section_department_id'=>$section_department_id))->column('major_id');
        json_success($info);
    }

    /**
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 修改科系
     */
    public function edit_section_department()
    {
        $name = input('name');
        $sort = input('sort', 1);
        $status = input('status', 1);
        $section_department_id = input('section_department_id');
        $major_ids = input('major_ids/a');

        if (!$major_ids) {
            json_fail('请先选择专业');
        }
        if (!$section_department_id) {
            json_fail('缺少科系id参数');
        }
        if (!$name) {
            json_fail('缺少标题参数');
        }
        //实例化数据库
        $section_department = new \app\admin\model\SectionDepartment();
        $section_department_major = new SectionDepartmentMajor();

        //要修改的数据
        $saveData = array(
            'name' => $name,
            'sort' => $sort,
            'status' => $status
        );

        $info = $section_department->where(array('id' => $section_department_id))->update($saveData);
        if ($info === false) {
            json_fail('修改失败');
        }

        //全部改为删除
        $status = $section_department_major->where(array('section_department_id'=>$section_department_id))->update(array('status'=>0));
        if($status == false){
            json_fail('修改失败');
        }
        foreach ($major_ids as $k=>$v){
            //首先查询有没有
            $majorInfo = $section_department_major->where(array('section_department_id'=>$section_department_id,'major_id'=>$v))->find();
            if($majorInfo){
                $section_department_major->where(array('section_department_id'=>$section_department_id,'major_id'=>$v))->update(array('status'=>1));
            }else{
                $addSDMData=  array(
                    'section_department_id'=>$section_department_id,
                    'major_id'=>$v
                );
                $section_department_major->insertGetId($addSDMData);
            }
        }

        json_success('修改成功');
    }

    /**
     * 删除科系
     * @author chengkaikai
     */
    public function delete_section_department()
    {
        $section_department_id = input('section_department_id');
        if (!$section_department_id) {
            json_fail('缺少科系id参数');
        }

        //实例化数据库
        $section_department = new \app\admin\model\SectionDepartment();

        //要修改的数据
        $saveData = array(
            'status' => 0
        );

        $info = $section_department->where(array('id' => $section_department_id))->update($saveData);
        if ($info === false) {
            json_fail('删除失败');
        }
        json_success('删除成功');
    }
}